/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.index;

import com.inspur.edp.lcm.metadata.api.entity.ExternalInfo;
import com.inspur.edp.lcm.metadata.api.entity.MetadataPackageHeader;
import com.inspur.edp.lcm.metadata.api.entity.MetadataProject;
import com.inspur.edp.lcm.metadata.core.entity.MdProjDependencySource;
import com.inspur.edp.lcm.metadata.core.entity.MdProjectDependencyNode;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MetadataPackageIndexServiceForProject extends MetadataPackageIndexService {
    private static MetadataPackageIndexServiceForProject instance;

    public static MetadataPackageIndexServiceForProject getInstance() {
        if (instance == null) {
            instance = new MetadataPackageIndexServiceForProject();
        }
        return instance;
    }

    /**
     * 根据元数据工程路径以及元数据工程信息（可不传）构造元数据工程对应依赖引用节点
     *
     * @param projectPath     元数据工程路径
     * @param metadataProject 元数据工程信息
     * @return 元数据工程对应依赖引用节点
     */
    public MdProjectDependencyNode getMetadataDependencyNode(String projectPath, MetadataProject metadataProject) {
        if (metadataProject == null) {
            metadataProject = projectService.getMetadataProjInfo(projectPath);
        }

        if (metadataProject == null) {
            log.error("获取BO内引用[{}]元数据工程失败!", projectPath);
            return null;
        }

        // 构建依赖节点基础信息
        MdProjectDependencyNode dependencyNode = new MdProjectDependencyNode();
        dependencyNode.setMetadataPackageName(metadataProject.getMetadataPackageInfo().getName());
        dependencyNode.setLocation(projectPath);
        dependencyNode.setSourceName(MdProjDependencySource.BO);
        // 读取依赖信息
        Set<String> metadataPackageRefs = null;
        if (metadataProject.getMetadataPackageRefs() != null) {
            metadataPackageRefs = metadataProject.getMetadataPackageRefs().stream()
                    .map(MetadataPackageHeader::getName)
                    .collect(Collectors.toSet());
        }

        dependencyNode.setMetadataPackageRefs(metadataPackageRefs);

        dependencyNode.setProjectRefs(metadataProject.getProjectRefs());

        dependencyNode.setMavenRefs(metadataProject.getMavenPackageRefs());

        // 读取依赖信息
        Set<String> mdpkgRefs = null;
        if (metadataProject.getMdpkgRefs() != null) {
            mdpkgRefs = metadataProject.getMdpkgRefs().stream()
                    .map(MetadataPackageHeader::getName)
                    .collect(Collectors.toSet());
        }

        dependencyNode.setMdpkgRefs(mdpkgRefs);

        Set<String> idpMetadataIds = null;
        if (metadataProject.getExternalRefs() != null) {
            idpMetadataIds = metadataProject.getExternalRefs().stream()
                    .map(ExternalInfo::getMetadataId)
                    .collect(Collectors.toSet());
        }

        dependencyNode.setIdpMetadataIds(idpMetadataIds);

        return dependencyNode;
    }
}
